<?php

namespace backend\modules\member\controllers;

use Yii;
use common\models\base\SearchModel;
use common\traits\MerchantCurd;
use common\enums\StatusEnum;
use common\models\member\Level;
use backend\controllers\BaseController;
use common\models\member\Recharge;

/**
 * 会员等级管理
 *
 * Class RechargeController
 * @author Maomao
 * @package backend\modules\member\controllers
 */
class RechargeController extends BaseController
{
    /**
     * @var \yii\db\Recharge
     */
    public $modelClass = Recharge::class;

    /**
     * 首页
     *
     * @return string
     * @throws \yii\web\NotFoundHttpException
     */
    public function actionIndex()
    {
        $searchModel = new SearchModel([
            'model' => $this->modelClass,
            'scenario' => 'default',
            'partialMatchAttributes' => ['bank_user_name','address','bank_name'], // 模糊查询
            'defaultOrder' => [
                'id' => SORT_DESC
            ],
            'pageSize' => $this->pageSize
        ]);

        $dataProvider = $searchModel
            ->search(Yii::$app->request->queryParams);

        return $this->render($this->action->id, [
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel,
        ]);
    }

    public function actionSuo()
    {  
      //锁定
        $id=Yii::$app->request->get('id');
        $status=Yii::$app->request->get('status');
        $model=Recharge::find()->where(['id'=>$id])->andWhere(['<=','status',1])->all();
        $connection = Yii::$app->db;
        $transaction = $connection->beginTransaction();
        try {
            foreach ($model as $key => $v) { 
                $v->status=$status;
                if (!$v->update()) {
                        throw new \Exception("审核失败:".$this->getError($v));
                }
            }
            $transaction->commit();/*提交事物*/
            return $this->message('成功', $this->redirect(Yii::$app->request->referrer), 'success');
        }
        catch (\Exception $e) {
            $transaction->rollBack();/*回滚*/
            return $this->message($e->getMessage(), $this->redirect(Yii::$app->request->referrer), 'error');
       }
    }

    public function actionView()
    {
        $id=Yii::$app->request->get('id');
        $model = $this->modelClass::findOne($id);
        return $this->render('view', [
            'model' => $model,
        ]);
    }

    public function actionCheckAccount(){
        $id=Yii::$app->request->post('id');
        $models=Recharge::find()->where(['in','id',$id])->andWhere(['status'=>1])->all();
        $tag = true;
        $connection = Yii::$app->db;
        $transaction = $connection->beginTransaction();
        try {
                foreach ($models as $k => $var) {
                
                    $var->ChongZhi();
                }
                if ($tag) {
                    $transaction->commit();/*提交事物*/

                    return $this->message('审核成功', $this->redirect(Yii::$app->request->referrer), 'success');
                }else{

                return $this->message('审核失败', $this->redirect(Yii::$app->request->referrer), 'error');
                }
        } catch (\Exception $e) {
            $transaction->rollBack();/*回滚*/
            return $this->message($e->getMessage(), $this->redirect(Yii::$app->request->referrer), 'error');
        }
    }
    public function actionRefuse(){
        $id=yii::$app->request->post('id');
        $cont=yii::$app->request->post('con');
        $connection = Yii::$app->db;
        $transaction = $connection->beginTransaction();
        $model=Recharge::find()->where(['in','id',$id])->andWhere(['status'=>1])->all();
        try {
            foreach ($model as $key => $v) { 
                
                    $v->status=3;
                    $v->updated_at=time();
                    $v->details=$cont;//打回原因
                    if (!$v->update()) {
                        throw new \Exception("打回失败");
                    }
            }
            $transaction->commit();/*提交事物*/
            return $this->message('打回成功', $this->redirect(Yii::$app->request->referrer), 'success');
        }
        catch (\Exception $e) {
            $transaction->rollBack();/*回滚*/
            return $this->message($e->getMessage(), $this->redirect(Yii::$app->request->referrer), 'error');
        }
    }
}